package com.redoor.app.stake.basicdata.mq.consumerService;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.redoor.app.stake.basicdata.model.BaseChargerversionInfo;
import com.redoor.app.stake.basicdata.model.BaseMqOperationLog;
import com.redoor.app.stake.basicdata.model.LogEnum;
import com.redoor.app.stake.basicdata.service.BaseChargerversionInfoService;
import com.redoor.app.stake.basicdata.service.LogService;
import com.redoor.app.stake.common.utils.UUIDTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Created by zhangYiHang on 2018/4/9.
 */
@Service
public class ChargePoleVersionMessageListener implements MessageListener {

    private static final Logger logger = LoggerFactory.getLogger(ChargePoleVersionMessageListener.class) ;

    @Autowired
    private BaseChargerversionInfoService baseChargerversionInfoService;
    @Autowired
    LogService logService;
    /**
     * 桩(枪)版本信息MQ监听
     * @Author zhangYiHang
     * @Date 2018/4/10 14:40
     * @Param
     */
    @Override
    public Action consume(Message message, ConsumeContext context) {
        String json = "";
        String tag = "";
        try {
            json = new String(message.getBody());
            tag = message.getTag();
        }catch (Exception e){
            e.printStackTrace();
        }
        logger.info("chargePole version list message listener get mq body = " + json) ;
        if(StringUtils.isEmpty(json)){
            logger.info("topic chargePoleVersionList message listener get empty mq tag=" + tag) ;
        } else {
            /*mq日志记录*/
            BaseMqOperationLog baseMqOperationLog = new BaseMqOperationLog();
            baseMqOperationLog.setUuid(UUIDTool.getUUID());
            baseMqOperationLog.setMessagetopic(message.getTopic());
            baseMqOperationLog.setMessagebody(message.getBody().toString());
            baseMqOperationLog.setMessagetag(message.getTag());
            baseMqOperationLog.setType(2);

            this.updateCharagerPoleVersionList(json,baseMqOperationLog) ;
        }
        return Action.CommitMessage ;
    }
    /**
     * 版本信息接收mq添加新数据或修改旧数据
     * @Author zhangYiHang
     * @Date 2018/4/10 14:37
     * @Param
     */
    public void updateCharagerPoleVersionList(String json,BaseMqOperationLog baseMqOperationLog){
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            String stakeno = jsonObject.getString("chargingPileNum");
            Integer firstVer = jsonObject.getInteger("masterVersion");
            Integer secondVer = jsonObject.getInteger("slaveVersion");
            if(stakeno == null || "".equals(stakeno)){
                logger.error("chargingPileNum is null, so not insert data ");
                return;
            }
            BaseChargerversionInfo baseChargerversionInfo = new BaseChargerversionInfo();
            baseChargerversionInfo.setUuid(UUIDTool.getUUID());
            baseChargerversionInfo.setStakeno(stakeno);
            baseChargerversionInfo.setFirstver(firstVer);
            baseChargerversionInfo.setSecondver(secondVer);
            if (0 < this.baseChargerversionInfoService.insertOrUpdateBaseChargerversionInfo(baseChargerversionInfo)){
                /*确认修改数据库之后保存mq日志到redis*/
                logService.addLogToRedis(baseMqOperationLog, LogEnum.MQ_LOG);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
